package com.check.operation.fragment;

import android.content.DialogInterface;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.check.operation.CheckApplication;
import com.check.operation.R;
import com.check.operation.base.BaseFragment;
import com.check.operation.model.Inpatient;
import com.check.operation.model.MessagRe;
import com.check.operation.model.Message;
import com.check.operation.model.Operation;
import com.check.operation.model.RequestUse;
import com.check.operation.service.SsxxAdapter;
import com.szj.util.HttpUtil;
import com.szj.util.JSONUtil;
import com.szj.util.SysUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 离开手术室
 */
@EFragment(R.layout.fragment_lksss)
public class LksssFragment extends BaseFragment implements AdapterView.OnItemClickListener {
    public String action;
    public static final String TAG = "Lksss";

    @App
    CheckApplication sshcApp;

    @ViewById
    TextView smh;

    @ViewById
    ListView sqlb;

    @ViewById
    TextView zyh;

    @ViewById
    TextView xm;

    @ViewById
    TextView xb;

    @ViewById
    TextView ks;

    String tmsmh;
    final String sszt = "l";
    final String ssztmc = "离开手术室";

    SsxxAdapter ssxxAdapter;

    List<Operation> operations;

    boolean checked = false;

    @AfterViews
    public void initview() {
        ssxxAdapter = new SsxxAdapter(getLayoutInflater());
    }

    @Background
    public void refdownsj(final String zyh) {
        try {
            initdata(zyh);
            String action = "patient/patientinfo";
            Map<String, Object> params = sshcApp.getParams();

            params.put("user", sshcApp.getUserRe().getUser());
            params.put("request", new Object() {
                public String barCode = zyh;
            });
            String rs = HttpUtil.postJSON(action, JSONObject.toJSONString(params));
            if (SysUtil.isNotNUll(rs)) {
                if (rs.indexOf("message") <= 0) {
                    Message message = (Message) JSONUtil.fromJson(rs, Message.class);
                    alert(message);
                } else {
                    MessagRe<Inpatient> inp = JSONObject.parseObject(rs, new TypeReference<MessagRe<Inpatient>>() {
                    });
                    operations = inp.value.operations;
                    uiInpat(inp);
                    if (operations != null && operations.size() == 1) {
                        Operation operation = operations.get(0);
                        action = "process/process";
                        RequestUse requestUse = new RequestUse();
                        requestUse.user = sshcApp.getUserRe().getUser();
                        Map<String, Object> params1 = new HashMap<String, Object>();
                        params1.put("surgicNo", operation.surgic_no);
                        params1.put("processType", sszt);
                        params1.put("position", operation.op_position);
                        params1.put("room", operation.op_room_id);
                        requestUse.request = params1;
                        rs = HttpUtil.postJSON(action, JSONObject.toJSONString(requestUse));
                        MessagRe<Inpatient> mereturn = JSONObject.parseObject(rs, new TypeReference<MessagRe<Inpatient>>() {
                        });
                        if ("success".equals(mereturn.message.code)) {
                            alert("状态更新为" + ssztmc);
                            operation.process_status = sszt;
                        } else {
                            alert(mereturn.message.text);
                        }
                    }
                    uiOpre(operations);
                }
            }
            sqlb.setOnItemClickListener(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @UiThread
    public void initdata(String tmh) {
        ssxxAdapter.setIndex(-1);
        ssxxAdapter.setCws(new ArrayList<Operation>());
        ssxxAdapter.notifyDataSetInvalidated();

        smh.setText(tmh);
        zyh.setText(null);
        xm.setText(null);
        xb.setText(null);
        ks.setText(null);

    }

    @UiThread
    public void uiInpat(MessagRe<Inpatient> inp) {
        Inpatient inpatient = inp.value;
        zyh.setText(inpatient.inpatient_no);
        xm.setText(inpatient.name);
        xb.setText(inpatient.sexname);
        ks.setText(inpatient.dept_name);
    }

    @UiThread
    public void uiOpre(List<Operation> operations) {
        for (Operation op : operations) {
            op.op_position_name = sshcApp.getDics().get("1021").get(op.op_position);
            op.process_status_name = sshcApp.getDics().get("1020").get(op.process_status);
        }
        ssxxAdapter.setCws(operations);
        sqlb.setAdapter(ssxxAdapter);
        ssxxAdapter.notifyDataSetChanged();
    }

    @UiThread
    public void alert(Message message) {
        Toast.makeText(getActivity(), message.text, Toast.LENGTH_SHORT).show();
    }

    @UiThread
    public void alert(String message) {
        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, final View arg1, int arg2, long arg3) {
        ssxxAdapter.setIndex(arg2);
        ssxxAdapter.notifyDataSetInvalidated();
        final Operation operation = operations.get(arg2);
        new AlertDialog.Builder(getActivity()).setTitle(ssztmc + "？")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            String action = "process/process";
                            RequestUse requestUse = new RequestUse();
                            requestUse.user = sshcApp.getUserRe().getUser();
                            Map<String, Object> params = new HashMap<String, Object>();
                            params.put("surgicNo", operation.surgic_no);
                            params.put("processType", sszt);
                            params.put("position", operation.op_position);
                            params.put("room", operation.op_room_id);
                            requestUse.request = params;
                            String rs = HttpUtil.postJSON(action, JSONObject.toJSONString(requestUse));
                            TextView t1 = (TextView) arg1.findViewById(R.id.sszt);
                            t1.setText(ssztmc);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                checked = false;
            }
        }).show();
    }
    @Override
    public void onGetBarValue(String arg0) {
        refdownsj(arg0);
    }
}
